package com.tyqx.biz.controller.eth.scheduler;


import com.tyqx.bean.model.EthPoolInfo;
import com.tyqx.bean.model.StopBlockInterval;
import com.tyqx.biz.controller.JsoupUtil;
import com.tyqx.service.EthPoolInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class SchedulerStopBlockInterval {

    private static final Logger logger = LoggerFactory.getLogger(SchedulerStopBlockInterval.class);


    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private EthPoolInfoService ethPoolInfoService;

    @Resource
    private JsoupUtil jsoupUtil;


    ///@Async
    //表示每隔秒
//    @Scheduled(fixedRate = 60 * 1000 * 10)
    @Scheduled(fixedDelay = 1000 * 60 * 10)
    public void testTasks() throws Exception {
        logger.debug("开始查询是否有断续的区块："+dateFormat.format(new Date())+"\n");
        try{
            List<StopBlockInterval> stopBlockIntervalList = ethPoolInfoService.selectStopBlockInterval();
            Long maxBlockNum = ethPoolInfoService.selectMaxBlock();

            Integer pageSize = 100;

            if(stopBlockIntervalList.size()>0){
                for (StopBlockInterval stopBlockInterval:stopBlockIntervalList) {
                    Long startBlockNum = stopBlockInterval.getStartnum();

                    Long endBlockNum = stopBlockInterval.getEndnum();

                    Long startToMaxBlockNum = maxBlockNum - startBlockNum;

                    Long endToMaxBlockNum = maxBlockNum - endBlockNum;

                    Long startPageIndex = startToMaxBlockNum/pageSize + 1;

                    Long endPageIndex = endToMaxBlockNum/pageSize + 1;

                    Long pageLength = startPageIndex - endPageIndex;

                    logger.debug("相差的页数："+pageLength);

                    if(startPageIndex.longValue() == endPageIndex.longValue()){
                        logger.debug("相等页数");
                        String viewBlockUrl = "https://cn.etherscan.com/blocks?ps=100&p="+startPageIndex;

                        String homeUrl = "https://cn.etherscan.com/";

                        List<EthPoolInfo> ethPoolInfoList = jsoupUtil.getEthPoolInfoList(viewBlockUrl,homeUrl);

                        if(ethPoolInfoList.size()>0){
                            List<EthPoolInfo> ethPoolInfos = new ArrayList<>();
                            for (int i=0;i<ethPoolInfoList.size();i++){
                                Long blockNum = ethPoolInfoList.get(i).getBlock();
                                if(ethPoolInfoService.selectEthInfoByBlockNum(blockNum)>0){

                                }else{
                                    ethPoolInfos.add(ethPoolInfoList.get(i));
                                }
                            }
                            if(ethPoolInfos.size()>0){
                                Integer flag = ethPoolInfoService.insertBatch(ethPoolInfos);
                                if(flag>0){
                                    logger.debug("获取页数："+startPageIndex+"的数据成功");
                                }else{
                                    logger.error("获取页数："+startPageIndex+"的数据失败");
                                }
                            }
                        }
                    }else if(pageLength>0){
                        pageLength = pageLength + 1;
                        logger.debug("多页："+pageLength);
                        Long currentPageIndex = new Long(0);
                        for(int j=0;j<pageLength;j++){
                            if(j==0){
                                currentPageIndex = endPageIndex;
                            }else{
                                currentPageIndex = currentPageIndex +1;
                            }


                            String viewBlockUrl = "https://cn.etherscan.com/blocks?ps=100&p="+currentPageIndex;

                            String homeUrl = "https://cn.etherscan.com/";

                            List<EthPoolInfo> ethPoolInfoList = jsoupUtil.getEthPoolInfoList(viewBlockUrl,homeUrl);

                            if(ethPoolInfoList.size()>0){
                                List<EthPoolInfo> ethPoolInfos = new ArrayList<>();
                                for (int i=0;i<ethPoolInfoList.size();i++){
                                    Long blockNum = ethPoolInfoList.get(i).getBlock();
                                    if(ethPoolInfoService.selectEthInfoByBlockNum(blockNum)>0){

                                    }else{
                                        ethPoolInfos.add(ethPoolInfoList.get(i));
                                    }
                                }
                                if(ethPoolInfos.size()>0){
                                    Integer flag = ethPoolInfoService.insertBatch(ethPoolInfos);
                                    if(flag>0){
                                        logger.debug("获取页数："+currentPageIndex+"的数据成功");
                                    }else{
                                        logger.error("获取页数："+currentPageIndex+"的数据失败");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.debug("结束查询是否有断续的区块："+dateFormat.format(new Date())+"\n");
    }
}